package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhentao.constant.MessageConstant;
import com.zhentao.dtos.Result;
import com.zhentao.mapper.UserMapper;
import com.zhentao.model.entity.system.SysUser;
import com.zhentao.service.UserService;
import com.zhentao.util.AppJwtUtil;
import com.zhentao.util.RedisClient;
import com.zhentao.util.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisClient redisClient;

    @Override
    public Integer getVerificationCode( String userMobile ) {
        Integer code = ValidateCodeUtils.generateValidateCode( 4 );
        System.err.println( userMobile + "验证码：" + code );
        redisClient.set( MessageConstant.VALIDATE_CODE + userMobile, code );
        redisClient.expire( MessageConstant.VALIDATE_CODE + userMobile, 60 );
        return code;
    }

    @Override
    public Result register( String phoneNumber, String password, String verificationCode ) {
        if (phoneNumber == null) {
            return Result.error( "手机号不能为空" );
        }
        if (password == null) {
            return Result.error( "密码不能为空" );
        }
        if (verificationCode == null) {
            return Result.error( "验证码不能为空" );
        }
        String redisCode = String.valueOf( redisClient.get( MessageConstant.VALIDATE_CODE + phoneNumber ) );
        if (redisCode == null) {
            return Result.error( "验证码已过期" );
        }
        if (! verificationCode.equals( redisCode )) {
            return Result.error( "验证码错误" );
        }
        SysUser sysUser = userMapper.selectOne( Wrappers.< SysUser > lambdaQuery().eq( SysUser::getPhone, phoneNumber ) );
        if (sysUser != null) {
            return Result.error( "手机号已被注册" );
        }
        sysUser = new SysUser();
        sysUser.setUsername( UUID.randomUUID().toString().replace( "-", "" ).substring( 0, 8 ) + phoneNumber );
        sysUser.setName( phoneNumber );
        sysUser.setAvatar( "http://47.97.103.171:19000/test/3f9a2ffb4bb9a54ded57f665e1ac960.jpg" );
        sysUser.setPhone( phoneNumber );
        String digestPassword = DigestUtils.md5DigestAsHex( password.getBytes() );
        sysUser.setPassword( digestPassword );
        sysUser.setStatus( 1 );
        userMapper.insert( sysUser );
        redisClient.del( MessageConstant.VALIDATE_CODE + phoneNumber );
        return Result.success();
    }

    @Override
    public Result login( String phoneNumber, String password ) {
        if (phoneNumber == null) {
            return Result.error( "手机号不能为空" );
        }
        if (password == null) {
            return Result.error( "密码不能为空" );
        }
        SysUser sysUser = userMapper.selectOne( Wrappers.< SysUser > lambdaQuery().eq( SysUser::getPhone, phoneNumber ) );
        if (sysUser == null) {
            return Result.error( "手机号未注册" );
        }
        String s = DigestUtils.md5DigestAsHex( password.getBytes() );
        System.err.println( s );
        if (! sysUser.getPassword().equals( s )) {
            return Result.error( "密码错误" );
        }
        String token = AppJwtUtil.getToken( sysUser.getId().longValue() );
        redisClient.set( token, sysUser );
        redisClient.expire( token, 1800 );

        return Result.success( "登陆成功", token );
    }
}
    